package com.hstcs.creator;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Animation.PlayMode;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.hstcs.assets.Area;
import com.hstcs.assets.Assets;
import com.hstcs.assets.Common;
import com.hstcs.assets.Config;
import com.hstcs.assets.FileName;
import com.hstcs.components.AnimationComponent;
import com.hstcs.components.BoundsComponent;
import com.hstcs.components.MgComponent;
import com.hstcs.components.MovementComponent;
import com.hstcs.components.StateComponent;
import com.hstcs.components.TextureComponent;
import com.hstcs.components.TransformComponent;
import com.hstcs.game.God;

public class MgCreator implements EntityCreator {

	public static Vector2 tmpVector2 = new Vector2();
	public static void create(int type){
		Entity entity = new Entity();
		AnimationComponent animation = getAnimation(type);
		
				
		MovementComponent movement = new MovementComponent();
		TransformComponent position = new TransformComponent();
		BoundsComponent bound = new BoundsComponent();	
		MgComponent mc = new MgComponent();
		mc.mg_type = type;
		
		StateComponent state = new StateComponent();
		TextureComponent texture = new TextureComponent();
		
		
		bound.bounds.width = Common.MG_WIDTH;
		bound.bounds.height = Common.MG_WIDTH;
		state.set(MgComponent.STATE_WALK);
		Area.setMgBornPos(position.pos);
		mc.srcPos.set(position.pos.x, position.pos.y );
		position.pos.z =2 ;
		position.speed = Common.MG_V;
		
		Area.setMgDest(mc.srcPos, mc.dest);
		mc.angleDest = mc.srcPos.angle(mc.dest) ;
	//	mc.angleDest = mc.dest.angle(mc.srcPos);
		tmpVector2.set(mc.dest.x - position.pos.x, mc.dest.y - position.pos.y);
		mc.angleDest = tmpVector2.angle() ;

//		position.rotation = tmpVector2.angle() + 90;
		if((mc.dest.x > mc.srcPos.x&& type ==Common.TYPE_GOU)||
				(mc.dest.x < mc.srcPos.x&& type == Common.TYPE_MAO)){
			texture.flip = true;
		}else{
			texture.flip = false;
		}
		position.rotation = 0;
		entity.add(mc);
		entity.add(state);
		entity.add(animation);
		entity.add(position);
		entity.add(movement);
		entity.add(texture);
		entity.add(bound);
		God.instance.engine.addEntity(entity);
	}
	
	public static AnimationComponent getAnimation(int type) {
		AnimationComponent animation = new AnimationComponent();
		if (type == Common.TYPE_MAO) {
			animation.animations.put(MgComponent.STATE_WALK,
					new Animation(0.4f, Assets.getMg(Common.TYPE_MAO), PlayMode.LOOP));
			animation.animations.put(MgComponent.STATE_CATCH,
					new Animation(0.4f, Assets.getMg(Common.TYPE_MAO), PlayMode.LOOP));
		}else if(type == Common.TYPE_GOU){
			animation.animations.put(MgComponent.STATE_WALK,
					new Animation(0.4f, Assets.getMg(Common.TYPE_GOU), PlayMode.LOOP));
			animation.animations.put(MgComponent.STATE_CATCH,
					new Animation(0.4f, Assets.getMg(Common.TYPE_GOU), PlayMode.LOOP));
		}
		return animation;
	}
}
